//
//  LeftVC.m
//  OrderDish
//
//  Created by mac on 16/11/25.
//  Copyright © 2016年 LiWenqing. All rights reserved.
//

#import "LeftVC.h"
#import "DetailViewController.h"
@interface LeftVC ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
    
    __weak IBOutlet UIScrollView *_scrollView;
    __weak IBOutlet UITableView *_menuTable;
    __weak IBOutlet UIImageView *titleImgV;
    __weak IBOutlet UILabel *countLabel;
    NSArray *_sectionArray;//区头数组
    NSMutableArray *_dishArray;//菜数组
    
    NSInteger _selectRow;
    NSInteger _selectSection;
}

@end

@implementation LeftVC
//我的菜单
- (IBAction)MyOrder:(id)sender {
    
}
//点菜
- (IBAction)OrderDish:(id)sender {
    
    static int a = 0;
    a++;
    countLabel.text = [NSString stringWithFormat:@"已经点了 %d 种菜",a];
    
    [UIView animateWithDuration:1 animations:^{
        
    } completion:^(BOOL finished) {
        
    }];
}
//详细
- (IBAction)detailButtonClick:(id)sender {
    DishModel *dish =[[_dishArray objectAtIndex:_selectSection] objectAtIndex:_selectRow];
    
    DetailViewController *dvc = [DetailViewController new];
    dvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    dvc.modalPresentationStyle = UIModalPresentationFormSheet;
    dvc.dish = dish;
    [self presentViewController:dvc animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    titleImgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"%dicon.png",_index+1]];
    _sectionArray=[[NSArray alloc]initWithArray:[DatabaseTool selectFromGroupTableWithGroupID:_index+1]];
    
    _dishArray=[[NSMutableArray alloc]init];
    for (NSString *ikind in _sectionArray) {
        NSArray *array=[DatabaseTool selectfromMenuTableWithIKind:ikind];
        [_dishArray addObject:array];
    }
    
    _menuTable.dataSource=self;
    _menuTable.delegate=self;
    _menuTable.separatorStyle=UITableViewCellSeparatorStyleNone;
    _menuTable.backgroundColor=[UIColor clearColor];
    
    _scrollView.delegate=self;
    _scrollView.pagingEnabled=YES;
    
    [self resetScrollView];
}
-(void)resetScrollView{
    NSArray *array=[_dishArray objectAtIndex:_selectSection];
    _scrollView.contentSize=CGSizeMake(_scrollView.frame.size.width * array.count, _scrollView.frame.size.height);
    for (UIView *view in _scrollView.subviews) {
        [view removeFromSuperview];
    }
    for (int i=0; i<array.count; i++) {
        DishModel *dish=[array objectAtIndex:i];
        UIImageView *imageView=[[UIImageView alloc]init];
        imageView.frame=CGRectMake(_scrollView.frame.size.width*i, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
        imageView.image=[UIImage imageNamed:dish.picName];
        [_scrollView addSubview:imageView];
    }
    [_scrollView setContentOffset:CGPointMake(0, 0)];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _selectRow=_scrollView.contentOffset.x/_scrollView.frame.size.width;
    [_menuTable reloadData];
    UITableViewCell *cell=[_menuTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectRow inSection:_selectSection]];
    [CommomTool flipCellWithCell:cell];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_selectSection==section) {
        return [[_dishArray objectAtIndex:section] count];
    }else{
        return 0;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sectionArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        UIImageView *image=[[UIImageView alloc]init];
        image.frame=cell.bounds;
        image.image=[UIImage imageNamed:@"line32"];
        image.tag=55;
        image.hidden=YES;
        [cell.contentView addSubview:image];
    }
    UIImageView *image=[cell.contentView viewWithTag:55];
    if (_selectRow==indexPath.row) {
        image.hidden=NO;
    }else{
        image.hidden=YES;
    }
    
    DishModel *dish=[[_dishArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@元/%@",dish.price,dish.unit];
    
    cell.textLabel.text = dish.name;

    cell.textLabel.textColor=[UIColor whiteColor];
    cell.detailTextLabel.textColor=[UIColor yellowColor];
    cell.backgroundColor=[UIColor clearColor];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(0, 0, tableView.frame.size.width, 50);
    [btn setTitle:[_sectionArray objectAtIndex:section] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"line31"] forState:UIControlStateNormal];
    btn.tag=section+2000;
    [btn addTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}
-(void)sectionBtnClick:(UIButton *)sender{
    _selectRow=0;
    
    NSMutableIndexSet *indexset=[NSMutableIndexSet indexSet];
    //上一次选择的区
    [indexset addIndex:_selectSection];
    
    _selectSection=sender.tag-2000;
    //当前选择的区
    [indexset addIndex:_selectSection];
    [_menuTable reloadSections:indexset withRowAnimation:UITableViewRowAnimationFade];
    [self resetScrollView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _selectRow=indexPath.row;
    [_menuTable reloadData];
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    [CommomTool flipCellWithCell:cell];
    
    [_scrollView setContentOffset:CGPointMake(_selectRow * _scrollView.frame.size.width, 0)];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
